#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$resw = @DesktopWidth
$resh = @DesktopHeight
$w = $resw / 2 - 150
$middleh = $resh / 2 - 200
$2h = $resh / 2 - 133.5

$GetWin = _WinGetHandleByExe('pol.exe')
$ffxiwinname = WinGetTitle(HWnd($GetWin))

$START = GUICreate("Blackguard Beta", 100, 400, 0, $middleh, $WS_POPUP, $WS_EX_TOPMOST)
$a = DllCall(".\inc\BMP2RGN.dll", "int", "BMP2RGN", _
		"str", ".\img\l1.bmp", _
		"int", 37, _
		"int", 73, _
		"int", 255)
SetWindowRgn($START, $a[0])
$selectlaunch = GUICtrlCreateLabel("", 0, 150, 100, 100)
GUICtrlSetTip(-1, "Select Your Bot")
$exit = GUICtrlCreateLabel("", 43, 371, 23, 21)
GUICtrlSetTip(-1, "Close Blackguard")
GUICtrlCreatePic(".\img\l1.bmp", 0, 0, 100, 400)
DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $START, "int", 500, "long", 0x00040001)
GUISetState()

$botselect = GUICreate("Blackguard Beta", 300, 267, $w, $2h, $WS_POPUP, $WS_EX_TOPMOST)
$a = DllCall(".\inc\BMP2RGN.dll", "int", "BMP2RGN", _
		"str", ".\img\select.bmp", _
		"int", 37, _
		"int", 73, _
		"int", 255)
SetWindowRgn($botselect, $a[0])
$chocobo = GUICtrlCreateLabel("", 72, 9, 151, 21)
GUICtrlSetTip(-1, "Fishing Bot")
$hunter = GUICtrlCreateLabel("", 161, 51, 131, 21)
GUICtrlSetTip(-1, "Hunting Bot")
$fish = GUICtrlCreateLabel("", 219, 123, 73, 21)
GUICtrlSetTip(-1, "Fishing Bot")
GUICtrlCreatePic(".\img\select.bmp", 0, 0, 300, 267)
GUISetState(@SW_HIDE, $botselect)

$BOTRUN = GUICreate("Blackguard Beta", 100, 400, 0, $middleh, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)
$a = DllCall(".\inc\BMP2RGN.dll", "int", "BMP2RGN", _
		"str", ".\img\l2.bmp", _
		"int", 37, _
		"int", 73, _
		"int", 255)
SetWindowRgn($BOTRUN, $a[0])
$run = GUICtrlCreateLabel("", 0, 33, 99, 95)
GUICtrlSetTip(-1, "Start Bot")
$stop = GUICtrlCreateLabel("", 0, 154, 99, 95)
GUICtrlSetTip(-1, "Close Bot")
$pause = GUICtrlCreateLabel("", 0, 273, 75, 55)
GUICtrlSetTip(-1, "Pause Bot")
$showsetup = GUICtrlCreateLabel("", 2, 373, 64, 19)
GUICtrlSetTip(-1, "Show Settings")
GUICtrlCreatePic(".\img\l2.bmp", 0, 0, 100, 400)
GUISetState(@SW_HIDE, $BOTRUN)

$botsetup = GUICreate("Blackguard Beta", 500, 400, 0, $middleh, $WS_POPUP, $WS_EX_TOPMOST)
$a = DllCall(".\inc\BMP2RGN.dll", "int", "BMP2RGN", _
		"str", ".\img\none.bmp", _
		"int", 37, _
		"int", 73, _
		"int", 255)
SetWindowRgn($botsetup, $a[0])
$hidesetup = GUICtrlCreateLabel("", 402, 374, 64, 19)
GUICtrlSetTip(-1, "Hide Settings")
GUICtrlCreatePic(".\img\none.bmp", 0, 0, 500, 400)
GUISetState(@SW_HIDE, $botsetup)


While 1
	$Message = GUIGetMsg()
	Select
		;Start GUI
		Case $Message = $GUI_EVENT_CLOSE Or $Message = $exit
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $START, "int", 500, "long", 0x00050002)
			Exit
		Case $Message = $selectlaunch
			GUISetState(@SW_SHOW, $botselect)
			;Botselect GUI
		Case $Message = $chocobo
			botselectdone()
			$bottype = "bgChocobo.exe"
			$botsetuptype = "Chocobo"
			botsetupfunc()
		Case $Message = $hunter
			botselectdone()
			$bottype = "bghunter.exe"
			$botsetuptype = "hunter"
			botsetupfunc()
		Case $Message = $fish
			botselectdone()
			$bottype = "bgfish.exe"
			$botsetuptype = "fish"
			botsetupfunc()
			;Botrun GUI
		Case $Message = $stop
			ProcessClose($bottype)
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $BOTRUN, "int", 200, "long", 0x00050002)
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $START, "int", 200, "long", 0x00040001)
			GUISetState(@SW_SHOW, $START)
		Case $Message = $run
			Run($bottype)
			Sleep(2000)
			WinActivate("FINAL FANTASY XI")
		Case $Message = $pause
			ProcessClose($bottype)
			ProcessClose($bottype)
			ProcessClose($bottype)
			ProcessClose($bottype)
		Case $Message = $showsetup
			ProcessClose($bottype)
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $BOTRUN, "int", 200, "long", 0x00050002)
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $botsetup, "int", 500, "long", 0x00040001)
			GUISetState(@SW_SHOW, $botsetup)
			;Botsetup GUI
		Case $Message = $hidesetup
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $botsetup, "int", 500, "long", 0x00050002)
			DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $BOTRUN, "int", 200, "long", 0x00040001)
			GUISetState(@SW_SHOW, $BOTRUN)


	EndSelect
WEnd

Func botsetupfunc()
	$botsetup = GUICreate("Blackguard Beta", 500, 400, 0, $middleh, $WS_POPUP, $WS_EX_TOPMOST)
	$a = DllCall(".\inc\BMP2RGN.dll", "int", "BMP2RGN", _
			"str", ".\img\" & $botsetuptype & ".bmp", _
			"int", 37, _
			"int", 73, _
			"int", 255)
	SetWindowRgn($botsetup, $a[0])
	$hidesetup = GUICtrlCreateLabel("", 402, 374, 64, 19)
	GUICtrlSetTip(-1, "Hide Settings")
	GUICtrlCreateSlider(200, 200, 150, 30)
	GUICtrlSetLimit(-1, 3, 1)
	GUICtrlCreatePic(".\img\" & $botsetuptype & ".bmp", 0, 0, 500, 400)
	GUISetState(@SW_HIDE, $botsetup)
EndFunc   ;==>botsetupfunc

Func botselectdone()
	GUISetState(@SW_HIDE, $botselect)
	DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $START, "int", 200, "long", 0x00050002)
	DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $BOTRUN, "int", 200, "long", 0x00040001)
	GUISetState(@SW_SHOW, $BOTRUN)
EndFunc   ;==>botselectdone

Func SetWindowRgn($h_win, $rgn)
	DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $rgn, "int", 1)
EndFunc   ;==>SetWindowRgn

Func _WinGetHandleByExe($sExe, $sTitle = '', $sText = '')
	Local $aPlist = ProcessList($sExe)
	Local $aWList = WinList(), $sHold
	For $iCC = 1 To UBound($aWList, 1) - 1
		For $xCC = 1 To UBound($aPlist, 1) - 1
			If WinGetProcess($aWList[$iCC][1]) = $aPlist[$xCC][1] Then
				If $sTitle Then
					If StringInStr($aWList[$iCC][0], $sTitle) Then
						Return $aWList[$iCC][1]
					EndIf
				ElseIf $sText Then
					If StringInStr(WinGetText($aWList[$iCC][0]), $sText) Then
						Return $aWList[$iCC][1]
					EndIf
				Else
					If BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][1]
				EndIf
			EndIf
		Next
	Next
	Return SetError(1, 0, '')
EndFunc   ;==>_WinGetHandleByExe